package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.pi.common.CodesConstants;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.*;
import com.pi.wms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/outbound/otso")
public class OtSoController extends BaseSoController<TwaDocSoHeader, Long> {

	@Autowired
	private OtSoService service;
	
	@Override
	protected ICrudService<TwaDocSoHeader, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "otso";
	}

	@Override
	protected TwaDocSoHeader newModel(HttpServletRequest request) {
		TwaDocSoHeader so=new TwaDocSoHeader();
		so.setCreateSource(CodesConstants.SYSTEM_WMS);
		so.setType(WmsConstants.SO_TYPE_WORK_ORDER);
		//so.setConsigneeId(XcmgAmConstants.CUSTOMER_ID_XCMG_AM);
		so.setConsigneeCode(XcmgAmConstants.CUSTOMER_CODE_XCMG_AM);
		so.setCategory(XcmgAmConstants.WORK_SOURCE_OTHER);
		so.setEditable(true);
		so.setEnabled(true);
		return so;
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		query.put("category", XcmgAmConstants.WORK_SOURCE_OTHER);
		super.prepareDatatableConditions(query, input);
	}

	@GetMapping({"edit/{id}"})
	public String edit(@ModelAttribute("m") TwaDocSoHeader m, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String flag=this.parameterService.stringValue(WmsConstants.PARAM_DOC_EDIT_ON_CREATE_SO);
		if  ((CodesConstants.COMMON_FLAG_YES.equalsIgnoreCase(flag))
				&& (!WmsConstants.ASN_STATUS_CREATE.equalsIgnoreCase(m.getStatus()))){
			this.addWarnMessage(redirectAttributes, new String[]{"单证已经开始作业，不能修改!"});
			return this.redirect("");
		}
		return super.edit(m,model,request,redirectAttributes);
	}
}
